﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.IO;
using System.Reflection;
using Opalis.QuickIntegrationKit;
using System.Management;

namespace Erlacher.Opalis.VMMExtended
{
    [OpalisObject("Rapid Provisioning", ShowFilters = false)]
    public class RapidProvisioning : IOpalisObject 
    {
        private Settings settings;
        private string s_name = string.Empty;
        private string s_vmguid = string.Empty;
        private string s_vmpath = string.Empty;
        private string s_computername = string.Empty;
        private string s_owner = string.Empty;
        private string s_Template = string.Empty;
        private string s_vmhost = string.Empty;
        private string s_vhdpath = string.Empty;
        private string s_description = string.Empty;
        private int i_size = 0;

        [OpalisConfiguration]
        public Settings Settings
        {
            set { settings = value; }
            get { return settings; }
        }

        [OpalisInput("VM GuidId")]
        public string VMGuid
        {
            set { s_vmguid = value; }
            get { return ""; }
        }

        [OpalisInput("VM Name")]
        public string Name
        {
            set { s_name  = value; }
            get { return ""; }
        }

        [OpalisInput("VM Path")]
        public string VMPath
        {
            set { s_vmpath = value; }
            get { return ""; }
        }

        [OpalisInput("Computer Name")]
        public string ComputerName
        {
            set { s_computername = value; }
            get { return ""; }
        }

        [OpalisInput("Owner")]
        public string Owner
        {
            set { s_owner = value; }
            get { return ""; }
        }

        [OpalisInput("Template Name")]
        public string TemplateName
        {
            set { s_Template = value; }
            get { return ""; }
        }

        [OpalisInput("VM Host Name")]
        public string VMHostName
        {
            set { s_vmhost = value; }
            get { return ""; }
        }

        [OpalisInput("VHD Path")]
        public string VHDPath
        {
            set { s_vhdpath = value; }
            get { return ""; }
        }

        [OpalisInput("Description")]
        public string Description
        {
            set { s_description = value; }
            get { return ""; }
        }
        public void Design(IOpalisDesigner designer)
        {
            designer.AddOutput("ErrorState");
            designer.AddOutput("ErrorMessage");
            designer.AddOutput("Trace");
           
        }

        public void Execute(IOpalisRequest request, IOpalisResponse response)
        {

            string psscript = "";
            PowerShellHelper.ReturnObject Results;


            Assembly _assembly = Assembly.GetExecutingAssembly();
            StreamReader _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("Erlacher.Opalis.VMMExtended.PSScripts.RapidProvisioning.ps1"));
            psscript = _textStreamReader.ReadToEnd();

            psscript = psscript.Replace("%COMPUTERNAME%", this.s_computername  );
            psscript = psscript.Replace("%TEMPLATE%", this.s_Template  );
            psscript = psscript.Replace("%VMGUID%", this.s_vmguid  );
            psscript = psscript.Replace("%VMNAME%", this.s_name);
            psscript = psscript.Replace("%DISCRIPTION%", this.s_description );
            psscript = psscript.Replace("%OWNER%", this.s_owner );
            psscript = psscript.Replace("%HOSTNAME%", this.s_vmhost );
            psscript = psscript.Replace("%VMPATH%", this.s_vmpath );
            psscript = psscript.Replace("%VHDPATH%", this.s_vhdpath );


            Results = PowerShellHelper.RunScript(psscript.ToString(), settings.DomainName + @"\" + settings.UserName , settings.Password , settings.VMMServer  );

            Dictionary<string, object> publishedData = new Dictionary<string, object>();

            publishedData.Add("ErrorState", Results.ErrorState.ToString());
            publishedData.Add("ErrorMessage", Results.ErrorMessage );
            publishedData.Add("Trace", Results.Trace );

            response.Publish(publishedData);
            
             
        }
    }
}